import { AcGameObject } from "/static/js/ac_game_object/base.js";

export class Player extends AcGameObject{
    constructor(root,info) {
        super();
        this.root = root;
        this.id = info.id; // 用于区分人物
        this.x = info.x;
        this.y = info.y;
        this.color = info.color;
        this.width = info.width;
        this.height = info.height;
        
        this.direction = 1; // 方向
        this.vx = 0;
        this.vy = 0;

        this.gravity = 50; // 重力 
        this.speedx = 400; // 移动时，水平方向速度
        this.speedy = -1000; // 跳起时，垂直方向速度
        this.status = 3;  // 0: idle, 1: 向前，2：向后，3：跳跃，4：攻击，5：被打，6：死亡
        this.ctx = this.root.game_map.ctx;
        this.pressed_keys = this.root.game_map.controller.pressed_keys;

        this.animations = new Map(); // 用来存储每一张gif
        this.frame_current_cnt = 0; // 每过一帧记录一次，统计记录了多少帧
        this.hp = 100;  // 血条
        this.$hp = this.root.$kof.find(`.kof-head .kof-head-player${this.id}-hp div`);// 对应的div血条消失
        console.log(this.$hp);
    }

    start() {

    }

    move() {
        // 只有在空中才有重力
       
             this.vy += this.gravity;
       
        this.x += this.vx * this.timedelta / 1000;
        this.y += this.vy * this.timedelta / 1000;
        if (this.y > 450) {
            this.y = 450;
            this.vy = 0;
            if (this.status === 3 ) {
                this.vy += this.gravity;
                this.status = 0;  // 一直在地平线
           }
            
        }

        // 边界处理
        if (this.x < 0) {
            this.x = 0;
        } else if (this.x + this.width > this.root.game_map.$canvas.width()){
            this.x = this.root.game_map.$canvas.width() - this.width;
        }
    }

    update_control() {
        let a,w,d,space;
        if (this.id == 0) {
            a = this.pressed_keys.has('a');
            w = this.pressed_keys.has('w');
            d = this.pressed_keys.has('d');
            space = this.pressed_keys.has(' ');
        } else {
            w = this.pressed_keys.has("ArrowUp");
            a = this.pressed_keys.has('ArrowLeft');
            d = this.pressed_keys.has('ArrowRight');
            space = this.pressed_keys.has('Enter');
        }

        if (this.status === 0 || this.status === 1) { // 静止、向前、向后移动时可以跳起
            if (space) {
                this.status = 4;
                this.vx = 0;
                this.frame_current_cnt = 0; // 初始化帧数
            } else if (w) {
                if (d) {
                    this.vx = this.speedx;
                } else if (a) {
                    this.vx = -this.speedx;
                } else {
                    this.vx = 0;
                }
                this.vy = this.speedy;
                this.status = 3; 
            } else if (d) {
                this.vx = this.speedx;    
                this.status = 1;
            } else if (a){
                this.vx = -this.speedx;
                this.status = 1;
            } else {
                this.vx = 0;
                this.status = 0;
            }
        }
       
    }

    update_direction() { // 交换方向
        if (this.status === 6) return;
        let players = this.root.player;
        if (players[0] && players[1]) {
            let me = this, you = players[1 - this.id];
            if (me.x < you.x) {
                me.direction = 1;
            } else {
                me.direction = -1;
            }
        }
    }
    is_attack() {
        if (this.status == 6) return;
        this.status = 5;
        this.frame_current_cnt = 0;
        this.hp = Math.max(this.hp - 20,0);

        this.$hp.animate({
            width: this.$hp.parent().width() * this.hp / 100
        })

        if (this.hp <= 0) {
            this.status = 6; // 死亡状态
            this.frame_current_cnt = 0;
        }
    }
    is_collision(r1, r2) {
        if (Math.max(r1.x1, r2.x1) > Math.min(r1.x2, r2.x2))
        {console.log("hello world")
            return false;  
        }
            
        if (Math.max(r1.y1, r2.y1) > Math.min(r1.y2, r2.y2))
            return false;

        console.log(r1.x1 + " " + r2.x1);
          
        return true;
        
    }

    update_attack() {
        if (this.status === 4 && this.frame_current_cnt === 18) { // 攻击动作的第18帧判断
            this.status = 0;
            let me = this, you =  this.root.player[1 - this.id];
            
            let r1;
            if (this.direction > 0) {
                r1 = {
                    x1:me.x + 120,
                    y1:me.y + 40,
                    x2:me.x + 120 + 100,
                    y2:me.y + 40 + 20,
                };
            } else {
                r1 = {
                    x1:me.x + me.width - 120 - 100,
                    y1:me.y + 40,
                    x2:me.x + me.width -  120 - 100 + 100,
                    y2:me.y + 40 + 20,
                };
            }

            let r2 = {
                x1: you.x,
                y1: you.y,
                x2: you.x + you.width,
                y2: you.y + you.height,
            };

             // 通过碰撞函数判断
            if (this.is_collision(r1,r2)) { 
                console.log("true");
                you.is_attack();
            }
        }
    }


    update() {
        this.update_control();
        this.update_attack();
        this.move();
        this.update_direction();
        this.render();
    }

    render() {
       

        let status = this.status; // 当前对象状态

        if (this.status === 1 && this.direction * this.vx < 0) status = 2; 

        let obj = this.animations.get(status); // 取出当前状态对应的gif动图

        // console.log(obj);

        if (obj && obj.loaded) {
            if (this.direction > 0) {
                let k = parseInt(this.frame_current_cnt / obj.frame_rate) % obj.frame_cnt;
                let image = obj.gif.frames[k].image;  //gif动图对应的第k帧
                // console.log(image);
                this.ctx.drawImage(image,this.x,this.y + obj.offset_y,image.width * obj.scale,image.height * obj.scale); // 渲染图片
            } else {
                this.ctx.save();  // 备份状态
                this.ctx.scale(-1,1); // 水平翻转
                this.ctx.translate(-this.root.game_map.$canvas.width(),0); 
                let k = parseInt(this.frame_current_cnt / obj.frame_rate) % obj.frame_cnt;
                let image = obj.gif.frames[k].image;  // gif动图对应的第k帧
                // console.log(image);
                this.ctx.drawImage(image,this.root.game_map.$canvas.width() - this.width - this.x,this.y + obj.offset_y,image.width * obj.scale,image.height * obj.scale); // 渲染图片
                
                this.ctx.restore(); // 复原状态
            }
          
        }

        if (status === 4 || status === 5 || status == 6) {
            if (this.frame_current_cnt == obj.frame_rate * (obj.frame_cnt - 1)) {

                if (status === 6) {
                    this.frame_current_cnt --;
                } else {
                     this.status = 0;
                }
               
            }
    
        }

        this.frame_current_cnt ++;
    }
}